package com.ts.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.utils.Result;
import com.ts.common.security.dto.UserLoginDto;
import com.ts.content.dto.PostDTO;
import com.ts.content.entity.DiscussEntity;
import com.ts.content.entity.LikeEntity;
import com.ts.content.entity.PostEntity;
import com.ts.content.service.DiscussService;
import com.ts.content.service.LikeService;
import com.ts.content.service.PostService;
import com.ts.content.service.UserService;
import com.ts.content.vo.Post2Vo;
import com.ts.content.vo.PostTopicVo;
import com.ts.content.vo.PostVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 帖子
 *
 * @author admin admin@qq.com
 * @since 1.0.0 2023-02-22
 */
@RestController
@RequestMapping("/post")
@Api(tags="帖子")
public class PostController {
    @Autowired
    private PostService postService;

    @Autowired
    private DiscussService discussService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    /**以下用于远程调用*/
    @PostMapping("/list")
    public IPage list(@RequestBody Page<PostVo> page){
        return postService.getPostList(page);
    }

    @PostMapping("/save")
    public Result<PostEntity> save(@RequestBody PostEntity postEntity){
        postService.save(postEntity);
        return new Result<>();
    }
    /**以上用于远程调用*/

    //根据类目查询帖子数据
    @GetMapping("/postList")
    public Result postList(Long page,@RequestParam("classId") Integer classId,@RequestParam("topicId") Integer topicId){
        Page<PostEntity> page1 = new Page();
        page1.setCurrent(page);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("class_id",classId);
        queryWrapper.eq("topic_id",topicId);
        queryWrapper.orderByDesc("create_time");

        //查询出帖子数据
        Page<PostEntity> page2 = postService.page(page1, queryWrapper);

        List<PostEntity> list = page2.getRecords();
        //将数据拷贝到零一集合中
        List<Post2Vo> postDTOList = new ArrayList<>();

        for (PostEntity postEntity : list) {
            Post2Vo post2Vo = new Post2Vo();
            BeanUtils.copyProperties(postEntity,post2Vo);
            String media = postEntity.getMedia();
            String discussList = postEntity.getDiscussList();

            if (!ObjectUtils.isEmpty(media)){
                post2Vo.setMedia(Arrays.asList(media.split(",")));
            }else {
                post2Vo.setMedia(Collections.emptyList());
            }

            if (!ObjectUtils.isEmpty(discussList)){
                post2Vo.setDiscussList(Arrays.asList(discussList.split(",")));
            }else {
                post2Vo.setDiscussList(Collections.emptyList());
            }

            post2Vo.setUserInfo(userService.getById(postEntity.getUserId()));
            postDTOList.add(post2Vo);
        }
        return new Result().Viewok(postDTOList);
    }

    //发布帖子
    @PostMapping("/postSave")
    public Result postSave(@RequestBody PostDTO postDTO, UserLoginDto userLoginDto ){
        //获取当前用户的id
        Integer userId = userLoginDto.getUserId();

        //判断是否有话题
        if(postDTO.getDiscussList().size()>0){
            discussService.hasDiscussList(userId,postDTO.getDiscussList());
        }

        //将dto数据拷贝到一个新的实体类中
        PostEntity postEntity = new PostEntity();
        BeanUtils.copyProperties(postDTO,postEntity);
        //保存到数据库中
        postEntity.setUserId(userId);
        postEntity.setMedia(postDTO.getMedia().stream().collect(Collectors.joining(",")));
        postEntity.setDiscussList(postDTO.getDiscussList().stream().collect(Collectors.joining(",")));
        postEntity.setCreateTime(new Date());
        postService.save(postEntity);

        return new Result().Viewok(postEntity.getId());
    }

    //获取当前用户加入的帖子列表
    @GetMapping("/postListByjoinTopic")
    public Result postListByjoinTopic(Long page,UserLoginDto userLoginDto){
        Integer userId = userLoginDto.getUserId();
        List<Post2Vo> post2VoPage = postService.getPostListByjoinTopic(page,userId,1);

        return new Result().Viewok(post2VoPage);
    }

    //根据当前用户查询帖子列表
    @GetMapping("/releaseList")
    public Result releaseList(UserLoginDto userLoginDto,Long page){
        Integer userId = userLoginDto.getUserId();

        List<Post2Vo> postListByjoinTopic = postService.getPostListByjoinTopic(page, userId, 2);
        return new Result().Viewok(postListByjoinTopic);
    }

    //根据帖子id查询帖子详情
    @GetMapping("/detail")
    public Result detail(Integer postId,UserLoginDto userLoginDto){
        PostTopicVo postTopicVo = postService.getPostTopicById(userLoginDto.getUserId(),postId);

        return new Result().Viewok(postTopicVo);
    }

    //点赞
    @PostMapping("/addCollection")
    public Result addCollection(UserLoginDto userLoginDto, @RequestBody LikeEntity likeEntity){
        likeEntity.setUserId(userLoginDto.getUserId());
        likeService.addCollection(likeEntity);
        return new Result();
    }

}